library(psych)
library(FAT)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(glue)
data <- read.csv('data/All.csv') %>%
  select(Gender, Country, Source, AMAS1:AMAS9) %>%
  mutate(
    Gender = factor(Gender,
                    levels = 1:2,
                    labels = c('Male', 'Female')),
    Country = factor(Country,
                     levels = 1:3,
                     labels = c('UK', 'Russia', 'China')),
    Total = rowSums(.[glue('AMAS{1:9}')]),
    Learning = rowSums(.[glue('AMAS{c(1, 3, 6, 7, 9)}')]),
    Testing = rowSums(.[glue('AMAS{c(2, 4, 8, 5)}')])
  ) %>%
  filter_at(vars(!!glue('AMAS{1:9}')), all_vars(. %in% 1:5))
AMAS_items <- matrix(
  c(
    'AMAS2', '2', 'Testing',  'Thinking about upcoming maths test',
    'AMAS4', '4', 'Testing',  'Taking examination in maths course',
    'AMAS8', '8', 'Testing',  'Being given an "pop" quiz in maths class',
    'AMAS5', '5', 'Testing', 'Being given a homework due the next class',
    'AMAS1', '1', 'Learning', 'Having to use tables',
    'AMAS3', '3', 'Learning', 'Watching teacher work equation',
    'AMAS6', '6', 'Learning', 'Listening to lecture in maths class',
    'AMAS7', '7', 'Learning', 'Listening to another student explain a formula',
    'AMAS9', '9', 'Learning', 'Starting a new chapter in maths book'
  ),
  ncol = 4,
  byrow = TRUE,
  dimnames = list(
    NULL, c('item', 'item_lab', 'scale', 'item_content')
  )
) %>%
  as.data.frame

UK

data %>%
  filter(Country == 'UK') %>%
  select(starts_with('AMAS')) %>%
  polychoric(correct = 0) %>%
  { .$rho } %>%
  scree(factors = FALSE)

data %>%
  filter(Country == 'UK') %>%
  select(starts_with('AMAS')) %>%
  fa(
    nfactors = 3, 
    rotate = 'varimax',
    fm = 'pa',
    cor = 'poly'
  ) %>%
  loadings2table(
    hints =
      setNames(
        glue(
          '{sc} - {ic}',
          sc = AMAS_items$scale,
          ic = AMAS_items$item_content),
        AMAS_items$item
      )
  )

Russia

data %>%
  filter(Country == 'Russia') %>%
  select(starts_with('AMAS')) %>%
  polychoric(correct = 0) %>%
  { .$rho } %>%
  scree(factors = FALSE)

data %>%
  filter(Country == 'Russia') %>%
  select(starts_with('AMAS')) %>%
  fa(
    nfactors = 3, 
    rotate = 'varimax',
    fm = 'pa',
    cor = 'poly'
  ) %>%
  loadings2table(
    hints =
      setNames(
        glue(
          '{sc} - {ic}',
          sc = AMAS_items$scale,
          ic = AMAS_items$item_content),
        AMAS_items$item
      )
  )

China

data %>%
  filter(Country == 'China') %>%
  select(starts_with('AMAS')) %>%
  polychoric(correct = 0) %>%
  { .$rho } %>%
  scree(factors = FALSE)

data %>%
  filter(Country == 'China') %>%
  select(starts_with('AMAS')) %>%
  fa(
    nfactors = 3, 
    rotate = 'varimax',
    fm = 'pa',
    cor = 'poly'
  ) %>%
  loadings2table(
    hints =
      setNames(
        glue(
          '{sc} - {ic}',
          sc = AMAS_items$scale,
          ic = AMAS_items$item_content),
        AMAS_items$item
      )
  )